package com.yaozheng.vocationaltraining.view;

import net.sf.json.JSONObject;

import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EViewGroup;
import org.androidannotations.annotations.ViewById;

import com.yaozheng.vocationaltraining.R;
import com.yaozheng.vocationaltraining.iview.IMyUserInfoView;
import com.yaozheng.vocationaltraining.utils.BitmapLoader;
import com.yaozheng.vocationaltraining.utils.Constants;
import com.yaozheng.vocationaltraining.utils.TypeUtils;
import com.yaozheng.vocationaltraining.widget.CircleImageView;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * 个人资料头像信息
 *
 * @author Administrator
 */
@EViewGroup(R.layout.view_my_user_info_head)
public class HeadMyUserInfoView extends RelativeLayout {

    @ViewById
    CircleImageView userAvatarImage;
    @ViewById
    TextView userNameText;

    IMyUserInfoView iMyUserInfoView;

    JSONObject myUserInfoJsonObject;

    public HeadMyUserInfoView(Context context, AttributeSet attrs,
                              int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // TODO Auto-generated constructor stub
    }

    public HeadMyUserInfoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
    }

    public HeadMyUserInfoView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
    }

    public void init(IMyUserInfoView iMyUserInfoView) {
        this.iMyUserInfoView = iMyUserInfoView;
    }

    /**
     * 加载数据
     *
     * @param myUserInfoJsonObject
     */
    public void loadData(JSONObject myUserInfoJsonObject,
                         BitmapLoader bitmapLoader) {
        this.myUserInfoJsonObject = myUserInfoJsonObject;
        userNameText.setText(TypeUtils.getJsonString(myUserInfoJsonObject,
                "name", ""));
        // ImageLoader.getInstance().displayImage(
        // TypeUtils.getJsonString(myUserInfoJsonObject, "avatar"),
        // userAvatarImage, avatarDisplayImageOptions);
        bitmapLoader.displayImage(TypeUtils.getJsonString(myUserInfoJsonObject, "avatar"),
                userAvatarImage);
    }

    @Click({R.id.changeAvatarImageView})
    public void userAvatarImageClcik() {
        if (iMyUserInfoView != null) {
            iMyUserInfoView.cutOutPhoto();
        }
    }

    /**
     * 查看大图
     */
    @Click(R.id.userAvatarImage)
    public void openBigPicture() {
        String imageUrl = TypeUtils.getJsonString(myUserInfoJsonObject, "avatar");
        if (imageUrl != null && !"".equals(imageUrl)) {
            Intent intent = new Intent();
            intent.setClassName(getContext(), Constants.BIG_PICTURE_ACTIVITY_CLASS_NAME);
            intent.putExtra("imageUrl", imageUrl);
            getContext().startActivity(intent);
        }
    }
}
